package com.san.metadata.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.san.common.utils.Constant;
import com.san.metadata.dao.BusinessDataDao;
import com.san.metadata.dao.ManageMetadataDao;
import com.san.metadata.domain.BusinessDataDO;
import com.san.metadata.domain.ColumnDTO;
import com.san.metadata.service.BusinessDataService;
import com.san.metadata.vo.BusinessDataVO;
/**
 * 业务元数据service实现类
 * @author SuperSR
 * @date 2019年3月8日 下午5:39:14
 *
 */
@Service
public class BusinessDataServiceImpl implements BusinessDataService{

	@Autowired
	private BusinessDataDao dao;
	@Autowired
	private ManageMetadataDao manageMetadataDao;
	
	@Override
	public BusinessDataDO getById(Long id) {
		return dao.getById(id);
	}

	@Override
	public BusinessDataDO getByName(String busiName) {
		return dao.getByName(busiName);
	}

	@Override
	public List<BusinessDataDO> list(Map<String, Object> map) {
		return dao.list(map);
	}

	@Override
	public int save(BusinessDataVO businessDataVO) {
		try{
			BusinessDataDO businessDataDO = dao.getByName(businessDataVO.getBusiName());
			if(businessDataDO != null){
				return -1;
			}else{
				int algorithmType = businessDataVO.getAlgorithmType();
				int linkWay = businessDataVO.getLinkWay();
				//TODO 调用查询表以及字段的方法
				String tableEname = "";
				String columnEname = "";
				String detColumnEname = "";
				String detColumnEnames = "";
				String busiSql = "";
				String busiSqldet = "";
				String dataColumn = "";
				String columnComment = "";
				if(Constant.BUSINESS_DATA_TYPE_LEGAL_PERSON == businessDataVO.getDataType()){//法人
					dataColumn = "legal_person_id";
				}else if(Constant.BUSINESS_DATA_TYPE_NATURAL_PERSON == businessDataVO.getDataType()){//自然人
					dataColumn = "natural_person_id";
				}
				//如果表id存在，则查出表英文名
				if(businessDataVO.getTableId() != null){
					tableEname = manageMetadataDao.getById(businessDataVO.getTableId()).getTargetTableName();
				}
				//如果字段id存在，则查出字段英文名
				if(businessDataVO.getColumnId() != null){
					columnEname = manageMetadataDao.getColumnById(businessDataVO.getColumnId()).getColumnName();
				}
				//若果明细字段id存在，则查出所有明细字段英文名
				String detColumnId = businessDataVO.getDetColumnId();
				if(detColumnId != null && !"".equals(detColumnId)){
					String[] detColumns = detColumnId.split(",");
					int i = 0;
					for (String detFId : detColumns) {
						ColumnDTO columnDTO = manageMetadataDao.getColumnById(Long.parseLong(detFId));
						detColumnEname = columnDTO.getColumnName();
						if(i == 0){
							detColumnEnames += detColumnEname;
							columnComment += columnDTO.getColumnComment();
						}else{
							detColumnEnames += "," + detColumnEname;
							columnComment += "," + columnDTO.getColumnComment();
						}
						i++;
					}
				}
				if(Constant.BUSI_DATA_ALGORITHMTYPE_SINGLE == algorithmType){//单项
					busiSql = "SELECT auto_id," + dataColumn + "," + columnEname+" FROM "+tableEname + " where " + dataColumn + " is not null and " + columnEname  + " is not null group by auto_id," + dataColumn + "," + columnEname + " order by " + dataColumn + " asc,auto_id desc";                  
					businessDataVO.setBusiSql(busiSql);
					if(Constant.BUSINESS_DATA_TYPE_LEGAL_PERSON == businessDataVO.getDataType()){//法人
						if(Constant.BUSI_DATA_LINKWAY_NATURAL == linkWay){//当算法类型为单项，且链接方式为自然人人信用记录时
							busiSqldet = "select natural_person_id from " + tableEname + " where " + dataColumn + " = ? ";
							businessDataVO.setBusiSqldet(busiSqldet);
						}
					}else if(Constant.BUSINESS_DATA_TYPE_NATURAL_PERSON == businessDataVO.getDataType()){//自然人
						if(Constant.BUSI_DATA_LINKWAY_LEGAL == linkWay){//当算法类型为单项，且链接方式为法人信用记录时
							busiSqldet = "select legal_person_id from " + tableEname + " where " + dataColumn + " = ? ";
							businessDataVO.setBusiSqldet(busiSqldet);
						} else if(Constant.BUSI_DATA_LINKWAY_NATURAL == linkWay){//当算法类型为单项，且链接方式为自然人人信用记录时
							busiSqldet = "select natural_person_id1 from " + tableEname + " where " + dataColumn + " = ? ";
							businessDataVO.setBusiSqldet(busiSqldet);
						}
					}
				}else if(Constant.BUSI_DATA_ALGORITHMTYPE_SINGLETABLE == algorithmType){//单表统计
					if(Constant.BUSINESS_DATA_TYPE_LEGAL_PERSON == businessDataVO.getDataType()){//法人
						if(Constant.BUSI_DATA_LINKWAY_DETAIL == linkWay){//当算法类型为单表统计，且有明细时
							busiSql = "SELECT " + dataColumn + ",count(" + dataColumn + ") from " + tableEname + " where " + dataColumn + " is not null group by " + dataColumn ;
							//busiSqldet ="select " + columnComment + " from dual union all SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							busiSqldet ="SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							businessDataVO.setBusiSql(busiSql);
							businessDataVO.setBusiSqldet(busiSqldet);
						}else if(Constant.BUSI_DATA_LINKWAY_NATURAL == linkWay){//当算法类型为单表统计，且连接方式为自然人信用记录
							busiSql = "SELECT " + dataColumn + ",count(" + dataColumn + ") from " + tableEname + " where " + dataColumn + " is not null group by " + dataColumn ;
							//busiSqldet ="select " + columnComment + " from dual union all SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							busiSqldet ="SELECT natural_person_id," + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							businessDataVO.setBusiSql(busiSql);
							businessDataVO.setBusiSqldet(busiSqldet);
						}else{//当算法类型为单表统计，且无明细时
							busiSql = "SELECT " + dataColumn + ",count(" + dataColumn + ") from "+tableEname+" where " + dataColumn + " is not null group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
						}
					}else if(Constant.BUSINESS_DATA_TYPE_NATURAL_PERSON == businessDataVO.getDataType()){//自然人
						if(Constant.BUSI_DATA_LINKWAY_DETAIL == linkWay){//当算法类型为单表统计，且有明细时
							busiSql = "SELECT " + dataColumn + ",count(" + dataColumn + ") from " + tableEname + " where " + dataColumn + " is not null group by " + dataColumn ;
							//busiSqldet ="select " + columnComment + " from dual union all SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							busiSqldet ="SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							businessDataVO.setBusiSql(busiSql);
							businessDataVO.setBusiSqldet(busiSqldet);
						}else if(Constant.BUSI_DATA_LINKWAY_LEGAL == linkWay){//当算法类型为单表统计，且连接方式为法人信用记录
							busiSql = "SELECT " + dataColumn + ",count(" + dataColumn + ") from " + tableEname + " where " + dataColumn + " is not null group by " + dataColumn ;
							//busiSqldet ="select " + columnComment + " from dual union all SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							busiSqldet ="SELECT legal_person_id," + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							businessDataVO.setBusiSql(busiSql);
							businessDataVO.setBusiSqldet(busiSqldet);
						}else if(Constant.BUSI_DATA_LINKWAY_NATURAL == linkWay){//当算法类型为单表统计，且连接方式为自然人信用记录
							busiSql = "SELECT " + dataColumn + ",count(" + dataColumn + ") from " + tableEname + " where " + dataColumn + " is not null group by " + dataColumn ;
							//busiSqldet ="select " + columnComment + " from dual union all SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							busiSqldet ="SELECT natural_person_id1," + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							businessDataVO.setBusiSql(busiSql);
							businessDataVO.setBusiSqldet(busiSqldet);
						}else{//当算法类型为单表统计，且无明细时
							busiSql = "SELECT " + dataColumn + ",count(" + dataColumn + ") from "+tableEname+" where " + dataColumn + " is not null group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
						}
					}
				}else if(Constant.BUSI_DATA_ALGORITHMTYPE_CUSTOMIZE == algorithmType){//自定义
					int a = businessDataVO.getBusiSql().indexOf("select ");
					int b = businessDataVO.getBusiSql().indexOf(" from");
					String column = businessDataVO.getBusiSql().substring(a+7, b).trim();
					if(!column.toLowerCase().startsWith(dataColumn)){
						return -4;
					}
					String[] disableSqls = {";","delete from","drop","insert into","create","update","alter table","grant","mysqladmin -u","rename","mysqldump -u","load_file("};
					busiSql = businessDataVO.getBusiSql();
					for (String disableSql : disableSqls) {
						if(busiSql.contains(disableSql)){
							return -3;
						}
					}
					if(businessDataVO.getBusiSqldet() != null && !"".equals(businessDataVO.getBusiSqldet())){
						busiSqldet = businessDataVO.getBusiSqldet();
						for (String disableSql : disableSqls) {
							if(busiSqldet.contains(disableSql)){
								return -3;
							}
						}
					}
				}else if(Constant.BUSI_DATA_ALGORITHMTYPE_SPLITSTATISTICS == algorithmType){//拆分统计
					if(Constant.BUSINESS_DATA_TYPE_LEGAL_PERSON == businessDataVO.getDataType()){//法人
						if(Constant.BUSI_DATA_LINKWAY_DETAIL == linkWay){//当算法类型为拆分统计，且有明细时
							busiSql = "SELECT " + dataColumn + "," + columnEname +" FROM "+tableEname +" where " + dataColumn + " is not null and " + columnEname + " = '" + businessDataVO.getColumnValue() + "' group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
							busiSqldet ="SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ? and " + columnEname + " = '" + businessDataVO.getColumnValue() + "'";
							businessDataVO.setBusiSqldet(busiSqldet);
						}else if(Constant.BUSI_DATA_LINKWAY_NATURAL == linkWay){//当算法类型为拆分统计，且连接方式为自然人信用记录
							busiSql = "SELECT " + dataColumn + "," + columnEname +" FROM "+tableEname +" where " + dataColumn + " is not null and " + columnEname + " = '" + businessDataVO.getColumnValue() + "' group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
							busiSqldet ="SELECT natural_person_id," + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ? and " + columnEname + " = '" + businessDataVO.getColumnValue() + "'";
							businessDataVO.setBusiSqldet(busiSqldet);
						}else{//当算法类型为拆分统计，且无明细时
							busiSql = "SELECT " + dataColumn + "," + columnEname +" FROM "+tableEname +" where " + dataColumn + " is not null and " + columnEname + " = '" + businessDataVO.getColumnValue() + "' group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
						}
					}else if(Constant.BUSINESS_DATA_TYPE_NATURAL_PERSON == businessDataVO.getDataType()){//自然人
						if(Constant.BUSI_DATA_LINKWAY_DETAIL == linkWay){//当算法类型为拆分统计，且有明细时
							busiSql = "SELECT " + dataColumn + "," + columnEname +" FROM "+tableEname +" where " + dataColumn + " is not null and " + columnEname + " = '" + businessDataVO.getColumnValue() + "' group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
							busiSqldet ="SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ? and " + columnEname + " = '" + businessDataVO.getColumnValue() + "'";
							businessDataVO.setBusiSqldet(busiSqldet);
						}else if(Constant.BUSI_DATA_LINKWAY_LEGAL == linkWay){//当算法类型为拆分统计，且连接方式为法人信用记录
							busiSql = "SELECT " + dataColumn + "," + columnEname + " FROM "+tableEname +" where " + dataColumn + " is not null and " + columnEname + " = '" + businessDataVO.getColumnValue() + "' group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
							busiSqldet ="SELECT legal_person_id," + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ? and " + columnEname + " = '" + businessDataVO.getColumnValue() + "'";
							businessDataVO.setBusiSqldet(busiSqldet);
						}else if(Constant.BUSI_DATA_LINKWAY_NATURAL == linkWay){//当算法类型为拆分统计，且连接方式为自然人信用记录
							busiSql = "SELECT " + dataColumn + "," + columnEname + " FROM "+tableEname +" where " + dataColumn + " is not null and " + columnEname + " = '" + businessDataVO.getColumnValue() + "' group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
							busiSqldet ="SELECT natural_person_id1," + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ? and " + columnEname + " = '" + businessDataVO.getColumnValue() + "'";
							businessDataVO.setBusiSqldet(busiSqldet);
						}else{//当算法类型为拆分统计，且无明细时
							busiSql = "SELECT " + dataColumn + "," + columnEname + " FROM "+tableEname +" where " + dataColumn + " is not null and " + columnEname + " = '" + businessDataVO.getColumnValue() + "' group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
						}
					}
					
					/*busiSql = "SELECT " + dataColumn + "," + columnEname+" FROM "+tableEname +" where " + dataColumn + " is not null and " + columnEname + " = '" + businessDataVO.getColumnValue() + "' group by " + dataColumn;
					businessDataVO.setBusiSql(busiSql);*/
				}
			}
		}catch (Exception e) {
			return -2;
		}
		return dao.save(businessDataVO);
	}

	@Override
	public int doEdit(BusinessDataVO businessDataVO) {
		try{
			BusinessDataDO businessDataDO = dao.getByName(businessDataVO.getBusiName());
			if(businessDataDO != null && !businessDataDO.getBusiName().equals(businessDataVO.getOldBusiName())){
				return -1;
			}else{
				int algorithmType = businessDataVO.getAlgorithmType();
				int linkWay = businessDataVO.getLinkWay();
				//TODO 调用查询表以及字段的方法
				String tableEname = "";
				String columnEname = "";
				String detColumnEname = "";
				String detColumnEnames = "";
				String busiSql = "";
				String busiSqldet = "";
				String dataColumn = "";
				String columnComment = "";
				if(Constant.BUSINESS_DATA_TYPE_LEGAL_PERSON == businessDataVO.getDataType()){//法人
					dataColumn = "legal_person_id";
				}else if(Constant.BUSINESS_DATA_TYPE_NATURAL_PERSON == businessDataVO.getDataType()){//自然人
					dataColumn = "natural_person_id";
				}
				
				//如果表id存在，则查出表英文名
				if(businessDataVO.getTableId() != null){
					tableEname = manageMetadataDao.getById(businessDataVO.getTableId()).getTargetTableName();
				}
				//如果字段id存在，则查出字段英文名
				if(businessDataVO.getColumnId() != null){
					columnEname = manageMetadataDao.getColumnById(businessDataVO.getColumnId()).getColumnName();
				}
				//若果明细字段id存在，则查出所有明细字段英文名
				String detColumnId = businessDataVO.getDetColumnId();
				if(detColumnId != null && !"".equals(detColumnId)){
					String[] detColumns = detColumnId.split(",");
					int i = 0;
					for (String detFId : detColumns) {
						ColumnDTO columnDTO = manageMetadataDao.getColumnById(Long.parseLong(detFId));
						detColumnEname = columnDTO.getColumnName();
						if(i == 0){
							detColumnEnames += detColumnEname;
							columnComment += columnDTO.getColumnComment();
						}else{
							detColumnEnames += "," + detColumnEname;
							columnComment += "," + columnDTO.getColumnComment();
						}
						i++;
					}
				}
				if(Constant.BUSI_DATA_ALGORITHMTYPE_SINGLE == algorithmType){//单项
					busiSql = "SELECT auto_id," + dataColumn + "," + columnEname+" FROM "+tableEname + " where " + dataColumn + " is not null and " + columnEname  + " is not null group by auto_id," + dataColumn + "," + columnEname + " order by " + dataColumn + " asc,auto_id desc";
					businessDataVO.setBusiSql(busiSql);
					if(Constant.BUSINESS_DATA_TYPE_LEGAL_PERSON == businessDataVO.getDataType()){//法人
						if(Constant.BUSI_DATA_LINKWAY_NATURAL == linkWay){//当算法类型为单项，且链接方式为自然人人信用记录时
							busiSqldet = "select natural_person_id from " + tableEname + " where " + dataColumn + " = ? ";
							businessDataVO.setBusiSqldet(busiSqldet);
						}
					}else if(Constant.BUSINESS_DATA_TYPE_NATURAL_PERSON == businessDataVO.getDataType()){//自然人
						if(Constant.BUSI_DATA_LINKWAY_LEGAL == linkWay){//当算法类型为单项，且链接方式为法人信用记录时
							busiSqldet = "select legal_person_id from " + tableEname + " where " + dataColumn + " = ? ";
							businessDataVO.setBusiSqldet(busiSqldet);
						} else if(Constant.BUSI_DATA_LINKWAY_NATURAL == linkWay){//当算法类型为单项，且链接方式为自然人人信用记录时
							busiSqldet = "select natural_person_id1 from " + tableEname + " where " + dataColumn + " = ? ";
							businessDataVO.setBusiSqldet(busiSqldet);
						}
					}
				}else if(Constant.BUSI_DATA_ALGORITHMTYPE_SINGLETABLE == algorithmType){//单表统计
					if(Constant.BUSINESS_DATA_TYPE_LEGAL_PERSON == businessDataVO.getDataType()){//法人
						if(Constant.BUSI_DATA_LINKWAY_DETAIL == linkWay){//当算法类型为单表统计，且有明细时
							busiSql = "SELECT " + dataColumn + ",count(" + dataColumn + ") from " + tableEname + " where " + dataColumn + " is not null group by " + dataColumn ;
							//busiSqldet ="select " + columnComment + " from dual union all SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							busiSqldet ="SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							businessDataVO.setBusiSql(busiSql);
							businessDataVO.setBusiSqldet(busiSqldet);
						}else if(Constant.BUSI_DATA_LINKWAY_NATURAL == linkWay){//当算法类型为单表统计，且连接方式为自然人信用记录
							busiSql = "SELECT " + dataColumn + ",count(" + dataColumn + ") from " + tableEname + " where " + dataColumn + " is not null group by " + dataColumn ;
							//busiSqldet ="select " + columnComment + " from dual union all SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							busiSqldet ="SELECT natural_person_id," + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							businessDataVO.setBusiSql(busiSql);
							businessDataVO.setBusiSqldet(busiSqldet);
						}else{//当算法类型为单表统计，且无明细时
							busiSql = "SELECT " + dataColumn + ",count(" + dataColumn + ") from "+tableEname+" where " + dataColumn + " is not null group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
						}
					}else if(Constant.BUSINESS_DATA_TYPE_NATURAL_PERSON == businessDataVO.getDataType()){//自然人
						if(Constant.BUSI_DATA_LINKWAY_DETAIL == linkWay){//当算法类型为单表统计，且有明细时
							busiSql = "SELECT " + dataColumn + ",count(" + dataColumn + ") from " + tableEname + " where " + dataColumn + " is not null group by " + dataColumn ;
							//busiSqldet ="select " + columnComment + " from dual union all SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							busiSqldet ="SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							businessDataVO.setBusiSql(busiSql);
							businessDataVO.setBusiSqldet(busiSqldet);
						}else if(Constant.BUSI_DATA_LINKWAY_LEGAL == linkWay){//当算法类型为单表统计，且连接方式为法人信用记录
							busiSql = "SELECT " + dataColumn + ",count(" + dataColumn + ") from " + tableEname + " where " + dataColumn + " is not null group by " + dataColumn ;
							//busiSqldet ="select " + columnComment + " from dual union all SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							busiSqldet ="SELECT legal_person_id," + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							businessDataVO.setBusiSql(busiSql);
							businessDataVO.setBusiSqldet(busiSqldet);
						}else if(Constant.BUSI_DATA_LINKWAY_NATURAL == linkWay){//当算法类型为单表统计，且连接方式为自然人信用记录
							busiSql = "SELECT " + dataColumn + ",count(" + dataColumn + ") from " + tableEname + " where " + dataColumn + " is not null group by " + dataColumn ;
							//busiSqldet ="select " + columnComment + " from dual union all SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							busiSqldet ="SELECT natural_person_id1," + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ?";
							businessDataVO.setBusiSql(busiSql);
							businessDataVO.setBusiSqldet(busiSqldet);
						}else{//当算法类型为单表统计，且无明细时
							busiSql = "SELECT " + dataColumn + ",count(" + dataColumn + ") from "+tableEname+" where " + dataColumn + " is not null group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
						}
					}
				}else if(Constant.BUSI_DATA_ALGORITHMTYPE_SINGLETABLE == algorithmType){//自定义
					int a = businessDataVO.getBusiSql().indexOf("select ");
					int b = businessDataVO.getBusiSql().indexOf(" from");
					String column = businessDataVO.getBusiSql().substring(a+7, b).trim();
					if(!column.toLowerCase().startsWith(dataColumn)){
						return -4;
					}
					String[] disableSqls = {";","delete from","drop","insert into","create","update","alter table","grant","mysqladmin -u","rename","mysqldump -u","load_file("};
					busiSql = businessDataVO.getBusiSql();
					for (String disableSql : disableSqls) {
						if(busiSql.contains(disableSql)){
							return -3;
						}
					}
					if(businessDataVO.getBusiSqldet() != null && !"".equals(businessDataVO.getBusiSqldet())){
						busiSqldet = businessDataVO.getBusiSqldet();
						for (String disableSql : disableSqls) {
							if(busiSqldet.contains(disableSql)){
								return -3;
							}
						}
					}
				}
				else if(Constant.BUSI_DATA_ALGORITHMTYPE_SPLITSTATISTICS == algorithmType){//拆分统计
					if(Constant.BUSINESS_DATA_TYPE_LEGAL_PERSON == businessDataVO.getDataType()){//法人
						if(Constant.BUSI_DATA_LINKWAY_DETAIL == linkWay){//当算法类型为拆分统计，且有明细时
							busiSql = "SELECT " + dataColumn + "," + columnEname +" FROM "+tableEname +" where " + dataColumn + " is not null and " + columnEname + " = '" + businessDataVO.getColumnValue() + "' group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
							busiSqldet ="SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ? and " + columnEname + " = '" + businessDataVO.getColumnValue() + "'";
							businessDataVO.setBusiSqldet(busiSqldet);
						}else if(Constant.BUSI_DATA_LINKWAY_NATURAL == linkWay){//当算法类型为拆分统计，且连接方式为自然人信用记录
							busiSql = "SELECT " + dataColumn + "," + columnEname +" FROM "+tableEname +" where " + dataColumn + " is not null and " + columnEname + " = '" + businessDataVO.getColumnValue() + "' group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
							busiSqldet ="SELECT natural_person_id," + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ? and " + columnEname + " = '" + businessDataVO.getColumnValue() + "'";
							businessDataVO.setBusiSqldet(busiSqldet);
						}else{//当算法类型为拆分统计，且无明细时
							busiSql = "SELECT " + dataColumn + "," + columnEname +" FROM "+tableEname +" where " + dataColumn + " is not null and " + columnEname + " = '" + businessDataVO.getColumnValue() + "' group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
						}
					}else if(Constant.BUSINESS_DATA_TYPE_NATURAL_PERSON == businessDataVO.getDataType()){//自然人
						if(Constant.BUSI_DATA_LINKWAY_DETAIL == linkWay){//当算法类型为拆分统计，且有明细时
							busiSql = "SELECT " + dataColumn + "," + columnEname +" FROM "+tableEname +" where " + dataColumn + " is not null and " + columnEname + " = '" + businessDataVO.getColumnValue() + "' group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
							busiSqldet ="SELECT " + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ? and " + columnEname + " = '" + businessDataVO.getColumnValue() + "'";
							businessDataVO.setBusiSqldet(busiSqldet);
						}else if(Constant.BUSI_DATA_LINKWAY_LEGAL == linkWay){//当算法类型为拆分统计，且连接方式为法人信用记录
							busiSql = "SELECT " + dataColumn + "," + columnEname + " FROM "+tableEname +" where " + dataColumn + " is not null and " + columnEname + " = '" + businessDataVO.getColumnValue() + "' group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
							busiSqldet ="SELECT legal_person_id," + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ? and " + columnEname + " = '" + businessDataVO.getColumnValue() + "'";
							businessDataVO.setBusiSqldet(busiSqldet);
						}else if(Constant.BUSI_DATA_LINKWAY_NATURAL == linkWay){//当算法类型为拆分统计，且连接方式为自然人信用记录
							busiSql = "SELECT " + dataColumn + "," + columnEname + " FROM "+tableEname +" where " + dataColumn + " is not null and " + columnEname + " = '" + businessDataVO.getColumnValue() + "' group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
							busiSqldet ="SELECT natural_person_id1," + detColumnEnames + " from " + tableEname + " where " + dataColumn + " is not null and " + dataColumn + " = ? and " + columnEname + " = '" + businessDataVO.getColumnValue() + "'";
							businessDataVO.setBusiSqldet(busiSqldet);
						}else{//当算法类型为拆分统计，且无明细时
							busiSql = "SELECT " + dataColumn + "," + columnEname + " FROM "+tableEname +" where " + dataColumn + " is not null and " + columnEname + " = '" + businessDataVO.getColumnValue() + "' group by " + dataColumn;
							businessDataVO.setBusiSql(busiSql);
						}
					}
				}
			}
		}catch (Exception e) {
			return -2;
		}
		return dao.doEdit(businessDataVO);
	}
	
	@Override
	public int remove(Long id) {
		return dao.remove(id);
	}

	@Override
	public int batchRemove(Long[] ids) {
		return dao.batchRemove(ids);
	}

	@Override
	public int count(Map<String, Object> map) {
		return dao.count(map);
	}

	@Override
	public int changeStatus(BusinessDataVO businessDataVO) {
		return dao.changeStatus(businessDataVO);
	}

	@Override
	public String getByDetFieldId(String id) {
		String detColumnEname = "";
		String detColumnEnames = "";
		try{
			if(id != null && !"".equals(id)){
				String[] detColumns = id.split(",");
				int i = 0;
				for (String detFId : detColumns) {
					detColumnEname = manageMetadataDao.getColumnById(Long.parseLong(detFId)).getColumnName();
					if(i == 0){
						detColumnEnames += detColumnEname;
					}else{
						detColumnEnames += "," + detColumnEname;
					}
					i++;
				}
			}
		} catch (Exception e){
			e.printStackTrace();
			return null;
		}
		return detColumnEnames;
	}

	@Override
	public List<BusinessDataDO> getByDataType(String dataType) {
		return dao.getByDataType(dataType);
	}

	@Override
	public List<BusinessDataDO> getByAlgorithmType(List<Integer> AlgorithmType) {
		return dao.getByAlgorithmType(AlgorithmType);
	}

}
